﻿using System;
using System.Collections.Generic;
using System.IO;

namespace DTO
{
    public class Keyword
    {
        #region Attributes
        int id;
        string keyword;
        int count;
        double score; 
        #endregion

        #region Properties
        public double Score
        {
            get
            {
                return score;
            }
            set
            {
                score = value;
            }
        }

        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public string KeywordText
        {
            get
            {
                return keyword;
            }
            set
            {
                keyword = value;
            }
        }

        public int Count
        {
            get
            {
                return count;
            }
            set
            {
                count = value;
            }
        } 
        #endregion

        #region Methods
        /// <summary>
        /// Export list of keywords to file: keyword ID + text + count
        /// </summary>
        /// <param name="vectorList">List of keywords</param>
        /// <param name="filePath">The path (with name) of the exported file</param>
        public static void ToKeywordFile(List<Keyword> keywordList, string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            foreach (Keyword curKeyword in keywordList)
            {
                sw.WriteLine(curKeyword.Id + " " + curKeyword.KeywordText + " " + curKeyword.Count);
            }
            sw.Close();
            fs.Close();
        }

        /// <summary>
        /// Import list of keywords from file: keyword ID + text + count
        /// </summary>
        /// <param name="filePath">The path (with name) of the imported file</param>
        /// <returns>List of imported keywords</returns>
        public static List<Keyword> FromKeywordFile(string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            List<Keyword> keywordList = new List<Keyword>();

            try
            {
                while (sr.Peek() >= 0)
                {
                    string line = sr.ReadLine();
                    string[] arrStr = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    Keyword curKeyword = new Keyword();
                    curKeyword.Id = Int32.Parse(arrStr[0]);
                    for (int i = 1; i < arrStr.Length - 1; i++)
                    {
                        curKeyword.KeywordText += arrStr[i] + " ";
                    }
                    curKeyword.KeywordText = curKeyword.KeywordText.Trim();
                    curKeyword.Count = Int32.Parse(arrStr[arrStr.Length-1]);
                    keywordList.Add(curKeyword);
                }
            }
            finally
            {
                sr.Close();
            }
            sr.Close();
            fs.Close();
            return keywordList;
        } 
        #endregion
    }
}
